//
//  KASliderHeader.swift
//  kano
//
//  Created by Frank on 2025/5/16.
//

import UIKit

class KASliderHeader: UIView {
    var min: Double = 0
    var max: Double = 600000
    var location: Double = 30000
    
    func updateRange(min: Double, max: Double) {
        self.min = min
        self.max = max
        self.var_minLabel.text = "Min \(min.separatorString())"
        self.var_maxLabel.text = "Max \(max.separatorString())"
    }
    
    lazy var var_title: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: kscale(14), weight: .bold)
        label.textColor = .color323135
        label.textAlignment = .left
        label.text = "Expected Credit Limit"
        return label
    }()
    
    lazy var var_currentLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: kscale(18), weight: .medium)
        label.textColor = .themeColor
        label.textAlignment = .left
        label.text = ""
        return label
    }()
    lazy var var_minLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: kscale(11), weight: .medium)
        label.textColor = UIColor.ka_hexColor(0x7D7D7D)
        label.textAlignment = .left
        label.text = "Min \(min.separatorString())"
        return label
    }()
    lazy var var_maxLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: kscale(11), weight: .medium)
        label.textColor = UIColor.ka_hexColor(0x7D7D7D)
        label.textAlignment = .left
        label.text = "Max \(max.separatorString())"
        return label
    }()

    open lazy var var_slider: UISlider = {
        let slider = UISlider(frame: CGRect(x: kscale(20), y: kscale(60), width: var_screenWidth - kscale(40), height: kscale(16)))
        // 设置滑动条范围（最小值、最大值）
        slider.minimumValue = Float(min)
        slider.maximumValue = Float(max)
        // 设置初始值
        slider.value = Float(location)
        
        // 修改轨道颜色
        slider.minimumTrackTintColor = .blue  // 左侧轨道颜色
        slider.maximumTrackTintColor = .gray  // 右侧轨道颜色

        // 修改滑块（thumb）样式
        slider.thumbTintColor = .red          // 滑块颜色
        slider.setThumbImage(UIImage(named: "expect_slider"), for: .normal) // 自定义滑块图片
        slider.setThumbImage(UIImage(named: "expect_slider"), for: .selected)
        slider.setThumbImage(UIImage(named: "expect_slider"), for: .highlighted)

        // 设置分段间隔（例如步长为10）
        slider.addTarget(self, action: #selector(sliderDidEndSliding), for: [.touchUpInside, .touchUpOutside])
        // 添加事件监听：滑动时触发
        slider.addTarget(self, action: #selector(sliderValueChanged(_:)), for: .valueChanged)
      
        return slider
    }()
    @objc func sliderValueChanged(_ sender: UISlider) {
        print("current value：\(sender.value)")
//        var_currentLabel.text = "\(Int(sender.value).separatorString())"
        let roundedValue = round(Double(sender.value) / location) * location
        var_currentLabel.text = "\(roundedValue.separatorString())"
    }
    @objc func sliderDidEndSliding() {
        let roundedValue = round(Double(var_slider.value) / location) * location
        var_slider.setValue(Float(roundedValue), animated: true)
        print("final value：\(roundedValue)")
        var_currentLabel.text = "\(roundedValue.separatorString())"
        let paramDic: [String : Any] = [
            "userId": KAUserManager.shared.user.softwareUpdateVersions,
            "minAmt": min,
            "maxAmt": max,
            "chooseMaxAmt": roundedValue,
            "chooseTime": Int(Date().timeIntervalSince1970),
            "uTime": Int(Date().timeIntervalSince1970),
        ]
        self.dataArray.append(paramDic)

        let btnInfo = KABtnInfo(eleId: "personinfo_scope", pageId: "individual_profile_summary", clickTime: Date.ka_currentData())
        KAReport.ka_reportWith(type: .btn, data: btnInfo.kj.JSONObject())
    }
    
    var dataArray: [Any] = []
    
    init() {
        super.init(frame: CGRectMake(0, 0, var_screenWidth, kscale(120)))
        backgroundColor = .white

        addSubview(var_title)
        addSubview(var_currentLabel)
        addSubview(var_minLabel)
        addSubview(var_maxLabel)
        addSubview(var_slider)
        
        var_title.snp.makeConstraints { make in
            make.top.equalTo(kscale(12))
            make.left.equalTo(kscale(20))
        }
        
        var_currentLabel.snp.makeConstraints { make in
            make.top.equalTo(var_title.snp.bottom).offset(kscale(10))
            make.right.equalTo(kscale(-20))
            make.left.equalTo(kscale(20))
        }
        
        var_minLabel.snp.makeConstraints { make in
            make.top.equalTo(var_slider.snp.bottom).offset(kscale(10))
            make.left.equalTo(kscale(20))
        }
        
        var_maxLabel.snp.makeConstraints { make in
            make.top.equalTo(var_slider.snp.bottom).offset(kscale(10))
            make.right.equalTo(kscale(-20))
        }
       
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
